package com.n5x.controller.admin.statistics;

import com.n5x.common.base.JsonModel;
import com.n5x.controller.BaseController;
import com.n5x.service.IStatisticsService;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前台购物车接口
 * 需要用户token
 */
@Controller("statistics")
@RequestMapping("/admin/statistics")
public class StatisticsController extends BaseController {

    @Autowired
    public HttpServletRequest request;

    @Autowired
    public IStatisticsService iStatisticsService;

    @RequestMapping(value = "/salesAmountByMonth")
    @ResponseBody
    public JsonModel salesAmountByMonth() {
        String ym = request.getParameter("ym");
        String begin = request.getParameter("begin");
        String end = request.getParameter("end");
        Map<String, Object> params = new HashMap();
        logger.info("[销售额月统计]ym:{},begin:{},end:{}", new Object[]{ym, begin, end});
        if (StringUtils.isNotBlank(ym)) {
            params.put("ym", ym);
        }
        if (StringUtils.isNotBlank(begin)) {
            params.put("begin", begin);
        }
        if (StringUtils.isNotBlank(end)) {
            params.put("end", end);
        }
        List list = iStatisticsService.countSalesAmountByMonth(params);
        return getResult(list);
    }

    @RequestMapping(value = "/salesAmountByDate")
    @ResponseBody
    public JsonModel salesAmountByDate() {
        String ym = request.getParameter("ym");
        String begin = request.getParameter("begin");
        String end = request.getParameter("end");
        Map<String, Object> params = new HashMap();
        logger.info("[销售额日统计]ym:{},begin:{},end:{}", new Object[]{ym, begin, end});
        if (StringUtils.isNotBlank(ym)) {
            params.put("ym", ym);
        }
        if (StringUtils.isNotBlank(begin)) {
            params.put("begin", begin);
        }
        if (StringUtils.isNotBlank(end)) {
            params.put("end", end);
        }
        List list = iStatisticsService.countSalesAmountByDate(params);
        return getResult(list);
    }

    @RequestMapping(value = "/goodsSalesByMonth")
    @ResponseBody
    public JsonModel goodsSalesByMonth() {
        String ym = request.getParameter("ym");
        String begin = request.getParameter("begin");
        String end = request.getParameter("end");
        logger.info("[商品月销售统计]ym:{},begin:{},end:{}", new Object[]{ym, begin, end});
        Map<String, Object> params = new HashMap();
        if (StringUtils.isNotBlank(ym)) {
            params.put("ym", ym);
        }
        if (StringUtils.isNotBlank(begin)) {
            params.put("begin", begin);
        }
        if (StringUtils.isNotBlank(end)) {
            params.put("end", end);
        }
        List<Map<String, Object>> list = iStatisticsService.countGoodsSalesByMonth(params);
        List<Map<String, Object>> data = this.formmatGoodsSales(list);
        return getResult(data);
    }

    @RequestMapping(value = "/goodsSalesByDate")
    @ResponseBody
    public JsonModel goodsSalesByDate() {
        String ym = request.getParameter("ym");
        String begin = request.getParameter("begin");
        String end = request.getParameter("end");
        logger.info("[商品日销售统计]ym:{},begin:{},end:{}", new Object[]{ym, begin, end});
        Map<String, Object> params = new HashMap();
        if (StringUtils.isNotBlank(ym)) {
            params.put("ym", ym);
        }
        if (StringUtils.isNotBlank(begin)) {
            params.put("begin", begin);
        }
        if (StringUtils.isNotBlank(end)) {
            params.put("end", end);
        }
        List<Map<String, Object>> list = iStatisticsService.countGoodsSalesByDate(params);
        List<Map<String, Object>> data = this.formmatGoodsSales(list);
        return getResult(data);
    }

    @RequestMapping(value = "/recharge")
    @ResponseBody
    public JsonModel recharge() {
        String ym = request.getParameter("ym");
        String begin = request.getParameter("begin");
        String end = request.getParameter("end");
        logger.info("[会员充值统计]ym:{},begin:{},end:{}", new Object[]{ym, begin, end});
        Map<String, Object> params = new HashMap();
        if (StringUtils.isNotBlank(ym)) {
            params.put("ym", ym);
        }
        if (StringUtils.isNotBlank(begin)) {
            params.put("begin", begin);
        }
        if (StringUtils.isNotBlank(end)) {
            params.put("end", end);
        }
        String total = iStatisticsService.recharge(params);
        return getResult(total);
    }

    @RequestMapping(value = "/sales")
    @ResponseBody
    public JsonModel sales() {
        String ym = request.getParameter("ym");
        String begin = request.getParameter("begin");
        String end = request.getParameter("end");
        logger.info("[会员消费统计]ym:{},begin:{},end:{}", new Object[]{ym, begin, end});
        Map<String, Object> params = new HashMap();
        if (StringUtils.isNotBlank(ym)) {
            params.put("ym", ym);
        }
        if (StringUtils.isNotBlank(begin)) {
            params.put("begin", begin);
        }
        if (StringUtils.isNotBlank(end)) {
            params.put("end", end);
        }
        String total = iStatisticsService.sales(params);
        return getResult(total);
    }


    @RequestMapping(value = "/twiceRechargeUsers")
    @ResponseBody
    public JsonModel twiceRechargeUsers() {
        logger.info("[会员2次充值统计]");
        List<Map<String, Object>> list = iStatisticsService.twiceRechargeUsers();
        return getResult(list);
    }

    /*-------------------------------------------------------------------------*/
    private List formmatGoodsSales(List<Map<String, Object>> list) {
        JSONObject jsonObject = new JSONObject();
        List data = new ArrayList();
        for (Map<String, Object> map : list) {
            String date = (String) map.get("PAY_DATE");
            if (jsonObject.get("PAY_DATE") == null || !date.equals(jsonObject.get("PAY_DATE"))) {
                jsonObject = new JSONObject();
                jsonObject.put("TOTAL", 0);
                jsonObject.put("PAY_DATE", date);
                jsonObject.put("GOODS", new ArrayList());
                data.add(jsonObject);
            }
            int qty = Integer.valueOf("" + map.get("QTY"));
            jsonObject.put("TOTAL", jsonObject.getInt("TOTAL") + qty);
            List this_ = (List) jsonObject.get("GOODS");
            this_.add(map);
            jsonObject.put("GOODS", this_);
        }
        return data;
    }
}